package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.Equipment;
import com.chain.qs.modules.biz.entity.EquipmentGroupItem;
import com.chain.qs.modules.biz.service.EquipmentGroupItemService;
import com.chain.qs.modules.biz.service.EquipmentGroupService;
import com.chain.qs.modules.biz.service.EquipmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 设备组所属设备Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/equgrpitem")
public class EquipmentGroupItemController extends BaseController {

    @Resource
    private EquipmentGroupItemService equipmentGroupItemService;

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private EquipmentGroupService equipmentGroupService;

    @ModelAttribute("item")
    public EquipmentGroupItem get(@RequestParam(required = false) String id, @RequestParam(value = "group.id", required = false) String groupId, @RequestParam(value = "equipment.id", required = false) String equipId) {
        EquipmentGroupItem item;
        if (StringUtils.isNotBlank(id)) {
            item = equipmentGroupItemService.get(id);
        } else {
            item = new EquipmentGroupItem();
            item.setGroup(equipmentGroupService.get(groupId));
            item.setEquipment(equipmentService.get(equipId));
        }
        return item;
    }

    @RequestMapping(value = {"list", ""})
    public String list(EquipmentGroupItem item, Model model) {
        model.addAttribute("list", equipmentGroupItemService.findList(item));
        return "modules/biz/equGrpItemList";
    }

    @RequestMapping(value = "form")
    public String from(Model model) {
        model.addAttribute("equipList", equipmentService.findList(new Equipment()));
        return "modules/biz/equGrpItemForm";
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public String save(@ModelAttribute("item") EquipmentGroupItem item) {
        if (equipmentGroupItemService.getOneByName(item) != 0) {
            return "errorName";
//        } else if (equipmentGroupItemService.getOneByKeyword(item) != 0) {
//            return "errorKeyword";
        } else {
            equipmentGroupItemService.save(item);
            return "success";
        }
    }

    @ResponseBody
    @RequestMapping(value = "itemList")
    public String itemList(EquipmentGroupItem item, Model model) {
        model.addAttribute("list", equipmentGroupItemService.findList(item));
        return "success";
    }


    @RequestMapping(value = "delete")
    public String delete(EquipmentGroupItem item, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/biz/equgrpitem?group.id=" + item.getGroup().getId();
        }
        equipmentGroupItemService.delete(item);
        addMessage(redirectAttributes, "删除设备组成功");
        return "redirect:" + adminPath + "/biz/equgrpitem?group.id=" + item.getGroup().getId();
    }
}
